﻿//资源ID(为－1时表示添加资源)
var ResID = -1;
//修改后的分类ID
var CateID = 0;
//修改前传进来的分类ID
var ECateID = 0;
var Name = "";
var Url = "";
var Description = "";
//是否编辑时空数据标记
var isEditEmpty = false;

//1显示ResourcesID层，0显示plhEdit层
function HiddenOrShow(rOrE)
{
    if(rOrE==0)
    {
        $get("plhEdit").style.display='';
        $get("ResourcesID").style.display='none';
    }
    else{
        $get("plhEdit").style.display='none';
        $get("ResourcesID").style.display='';
    }
}
//初始plhEdit层数据
function onsuccEdit(obj)
{
    if(obj!=null)
    {
        $get("txtName").value=obj.Name;
        $get("txtUrl").value=obj.Url;
        $get("CatSelect").value=obj.CateID;
        $get("txtDes").value=obj.Description;
    }
    HiddenOrShow(0);
}
//添加资源
function AddRes(cateID)
{
    ResID = -1;
    ECateID=cateID;    
    $get("CatSelect").value=cateID;
    $get("txtName").value="";
    $get("txtUrl").value="";    
    $get("txtDes").value="";
    HiddenOrShow(0);
}
//编辑资源
function EditRes(resID,cateID)
{
    ResID = resID;
    ECateID = cateID;
    WebServices.WWW.Resources.GetResourceByResID(resID,onsuccEdit);    
    
}
//删除资源
function DeleteRes(resID,cateID)
{
    if(Memberno=="")
    {
        top.location = "/user/login.htm?IsShow=1&ReturnUrl="+top.location.href;
        return;
    }
    CateID=cateID;
    ECateID=cateID;
    showProgressBar(false,true);
    $("$divIput$").style.display = "none";
    var title = GP066;
    var body="<p><strong>"+C1008+"</strong></p><br/><div class='Popup_button'><input type='button' value='"+C0069+"' id='addOkButton' class='button' onclick=\"opWebService('"+resID+"')\" />&nbsp;&nbsp;<input type='button' value='"+gCancel+"' id='addCancelButton' class='buttong' onclick='webFailure()'/></div>";
    var obj = {}
    $ShowMessageBox$(title,body,obj,webFailure,200);
}
//执行webservice方法，删除资源
function opWebService(resID)
{
    WebServices.WWW.Resources.DeleteResource(resID,InitDataSave,webFailure);
}
//调用webService方法失败
function webFailure()
{
    $EndMessageBox$();
}
//function DeleteRes(resID,cateID,obj)
//{
//    if(Memberno=="")
//    {
//        top.location = "/user/login.htm?IsShow=1&ReturnUrl="+top.location.href;
//        return;
//    }
//    CateID=cateID;
//    ECateID=cateID;
//    Sys.UI.DomElement.removeCssClass($get("DeletePanel"), "DisplayNone");
//    $create(AjaxControlToolkit.ModalPopupBehavior, 
//    {
//        "BackgroundCssClass":"modalBackground","CancelControlID":"CancelButton5","OnCancelScript":"onCancel('ModalPopup5')",
//        "DropShadow":false,"DynamicServicePath":null,"OkControlID":"OkButton5","OnOkScript":"onOk('ModalPopup5',"+resID+")",
//        "PopupControlID":"DeletePanel","PopupDragHandleControlID":"DeletePanelTitle","id":"ModalPopup5"
//    }, null, null, obj);
//    $find("ModalPopup5").show();
//}
////确定删除
//function onOk(eid,resID)
//{
//    var cmp = $find(eid);
//    if(cmp) $get(cmp.get_OkControlID()).disabled = true;    
//    if(eid=="ModalPopup5")
//    {
//       WebServices.WWW.Resources.DeleteResource(resID,InitDataSave);
//    }
//    DisposeModal(eid);
//}
//function onCancel(eid)
//{
//    DisposeModal(eid);
//}
//function DisposeModal(eid)
//{
//    var cmp = eid;
//    if (typeof eid == 'string')
//    {
//        cmp = $find(eid);
//    }
//    if(cmp)
//    {
//        var PopupControlID = cmp.get_PopupControlID();
//         $get(cmp.get_OkControlID()).disabled = false;
//        cmp.dispose();
//        Sys.UI.DomElement.addCssClass($get(PopupControlID), "DisplayNone");
//    }
//}
//根新ResourceSortID
function RaiseRes(resID,cateID)
{
    if(Memberno=="")
    {
        top.location = "/user/login.htm?IsShow=1&ReturnUrl="+top.location.href;
        return;
    }
    CateID=cateID;
    ECateID=cateID;
    WebServices.WWW.Resources.UpdateResourceSortID(resID,InitDataSave);
}
//更新ResourceActive
function DisableEnable(resID,cateID,active)
{
    if(Memberno=="")
    {
        top.location = "/user/login.htm?IsShow=1&ReturnUrl="+top.location.href;
        return;
    }
    CateID=cateID;
    ECateID=cateID;    
    WebServices.WWW.Resources.UpdateResourceActive(resID,active==1?false:true,InitDataSave);
}
//保存（增加修改资源）
function Save()
{
    if(Memberno=="")
    {
        top.location = "/user/login.htm?IsShow=1&ReturnUrl="+top.location.href;
        return;
    } 
    if(!$ValidButton$())return;
    //if(!validationForm(new validationOptions('btnSubmit', '', true, ''))) return;
    
    var name = $get("txtName").value;
    var url = $get("txtUrl").value;
    CateID = $get("CatSelect").value=="" ? 0 : $get("CatSelect").value;    
    var description = $get("txtDes").value;    
    if(ResID == -1)
    {
        WebServices.WWW.Resources.AddResource(PageID,CateID,url,name,description,LanLearn,LanBase,0,true,0,InitDataSave);
    }
    else
    {//int resID, int cateID, string url, string name, string desc    
        WebServices.WWW.Resources.UpdateResources(ResID,CateID,url,name,description,InitDataSave);
    }
}
//操作之后重新获取数据
function InitDataSave(result)
{
    if(result.State)
    { 
        WebServices.WWW.Resources.GetResourcesListByPC(PageID,CateID,RendResources);
        //如果修改前的CateID和修改后的CateID不同，再把修改前的数据重新获取
        if(ResID!=-1 && ECateID!=CateID)
        {
            isEditEmpty=true;
            WebServices.WWW.Resources.GetResourcesListByPC(PageID,ECateID,RendResources);                    
        }
    }
    else{
        HiddenOrShow(1);
    }
     $EndMessageBox$();
}
//替换数据
function RendResources(list)
{
    var listLength = list.length;
    if(listLength>0)
    {
        CateID=list[0].CateID;
        var domDiv = $get("ResL"+CateID);        
        if(domDiv != null)
        {
            if(CateID==0)
            {
                if($get("ResL0Item")==null) return;
                var item= $get("ResL0Item").innerHTML;
                var listBuilder = new Sys.StringBuilder();
                for (var index = 0; index < listLength; index++) 
                {              
                    var obj = list[index];                
                    var tmp = item.replace(/\$ResID\$/gi,obj.ResID).replace(/\$CateID\$/gi,obj.CateID).replace(/\$Url\$/gi,obj.Url).replace(/\$Description\$/gi,obj.Description);
                    listBuilder.append(tmp);
                }
                var sTemp = listBuilder.toString();
                domDiv.innerHTML = sTemp;
            }
            else
            {
                if($get("ResLItem")==null) return;
                var item= $get("ResLItem").innerHTML;
                var listBuilder = new Sys.StringBuilder();
                for (var index = 0; index < list.length; index++) 
                {              
                    var obj = list[index];                
                    var tmp = item.replace(/\$Active\$/gi,obj.Active==true?"<img src='/Images/icon_res_enable.gif' align='absmiddle' border='0' title='Disable'  />":"<img src='/Images/icon_res_disable.gif' align='absmiddle' border='0' title='Enable' />");
                    tmp = tmp.replace(/\$ResID\$/gi,obj.ResID).replace(/\$CateID\$/gi,obj.CateID).replace(/\$Url\$/gi,obj.Url).replace(/\$Name\$/gi,obj.Name).replace(/\$Description\$/gi,obj.Description).replace(/\$BActive\$/gi,obj.Active==true?1:0);
                    listBuilder.append(tmp);
                }
                var sTemp = listBuilder.toString();
                domDiv.innerHTML = sTemp;
            }         
        }        
    }
    else
    {
        if(isEditEmpty)
        {
            $get("ResL"+ECateID).style.display='none';
            isEditEmpty=false;
        }
        else{
            $get("ResL"+CateID).style.display='none';
        }
    }    
    HiddenOrShow(1);
}
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();
